'''
Created on 2013. 5. 16.

@author: Lee
'''
from google.appengine.ext import ndb
from kr.community.configure import JINJA_ENVIRONMENT
from kr.community.gaesessions import get_current_session
from kr.community.models import Users, ChannelSession
import webapp2

class LogoutPage(webapp2.RequestHandler):
    '''
        Logout Page
    '''


    def get(self):
        session = get_current_session()            
        
        template_values = {}
        if session.is_active():
            user_entry = ndb.Key(urlsafe=session['user']).get()
            channel_list = ChannelSession.query(ChannelSession.user_nickname==user_entry.user_nickname)
        
            if channel_list.count() != 0:
                for channel in channel_list:
                    channel.key.delete()
                
            if session.has_key('chat'):
                del session['chat']
                
            session.terminate()
            template_values['msg'] = "Logout is success."
        else:
            template_values['msg'] = "You're not logged on."
        template = JINJA_ENVIRONMENT.get_template('result_msg.html')
        
        self.response.write(template.render(template_values))
        
    def post(self):
        self.get()